﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.HumanResources;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ThridGroup.ERP.WarehouseManagement
{
    /// <summary>
    /// 供应商退货仓储
    /// </summary>
    public class SupplierReturnsServices : ApplicationService, ISupplierReturnsServices
    {
        /// <summary>
        /// 供应商退货
        /// </summary>
        private readonly IRepository<SupplierReturnsModel, int> _repository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository">供应商退货</param>
        public SupplierReturnsServices(IRepository<SupplierReturnsModel, int> repository)
        {
            this._repository = repository;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="supplierReturns"></param>
        /// <returns></returns>
        public async Task<int> AddSupplierReturns(SupplierReturnsDTO supplierReturns)
        {
            var entity = ObjectMapper.Map<SupplierReturnsDTO, SupplierReturnsModel>(supplierReturns);
            var result = await _repository.InsertAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task DelSupplierReturns(List<int> ids)
        {
            await _repository.DeleteDirectAsync(m => ids.Contains(m.Id));
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SupplierReturnsDTO> GetSupplierReturns(int id)
        {
            var supplierReturns = await _repository.FindAsync(id);
            var result = ObjectMapper.Map<SupplierReturnsModel, SupplierReturnsDTO>(supplierReturns);
            return result;
        }
        /// <summary>
        /// 列表显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="returnName"></param>
        /// <returns></returns>
        public async Task<List<SupplierReturnsDTO>> ShowSupplierReturns(int pageIndex = 1, int pageSize = 10, string? returnName = "")
        {
            var list = await _repository.GetListAsync();
            list = list.WhereIf(!string.IsNullOrEmpty(returnName), m => m.ReturnName.Contains(returnName)).ToList();
            var result = ObjectMapper.Map<List<SupplierReturnsModel>, List<SupplierReturnsDTO>>(list);
            return result;
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="supplierReturns"></param>
        /// <returns></returns>
        public async Task<int> UpdateSupplierReturns(SupplierReturnsDTO supplierReturns)
        {
            var entity = ObjectMapper.Map<SupplierReturnsDTO, SupplierReturnsModel>(supplierReturns);
            var result = await _repository.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }
    }
}
